﻿using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using GettingStartedSample.Models;
using Sport.Site.Models;

namespace Sport.Site.Models
{
	[ActiveRecord]
	public class Player : ActiveRecordBase, IEquatable<Player>, IPlayer
	{
		private readonly List<Meeting> meetings = new List<Meeting>();

		[PrimaryKey]
		public Int64 Id { get; set; }

		[Property]
		public string Tag { get; set; }

		[HasMany]
		public IList<Meeting> Meetings { get; set; }

		[HasAndBelongsToMany(typeof(Meeting),
			Table = "PlayerMeeting", ColumnKey = "playerId", ColumnRef = "meetingId", Inverse = true)]
		public IList<Meeting> MyMeetings { get; set; }

		public void HostMeeting(Meeting meeting)
		{
			meetings.Add(meeting);
		}

		public IList<Meeting> AllMeetings
		{
			get
			{
				List<Meeting> list = new List<Meeting>(MyMeetings);
				list.AddRange(Meetings);
				return list;
			}
		}

		public static List<Player> FindAll()
		{
			return new List<Player>((IEnumerable<Player>) FindAll(typeof (Player)));
		}

		public static Player FindById(Int64 id)
		{
			return (Player) FindByPrimaryKey(typeof (Player), id);
		}

		public bool Equals(Player other)
		{
			return this.Id == other.Id;
		}
	}
}